import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: elin11
 * Date: Jul 28, 2008
 * Time: 2:56:55 PM
 * To change this template use File | Settings | File Templates.
 */
 public class Rule{
    public String regRule;
    public ArrayList<String> RuleExceptions;
    public boolean cTerminus;
    public Rule(String rr, boolean cterm){
        regRule = rr;
        cTerminus = cterm;
        RuleExceptions = new ArrayList<String>();
    }
    public void addException(String rEx){
        RuleExceptions.add(rEx);
    }
    public boolean equals(Object rule){
    	if(rule.getClass().equals(this.getClass())){
    		Rule o = (Rule)rule;
    		System.out.println(regRule + " " + cTerminus + " : " + ((Rule)o).regRule+ " " + o.cTerminus);
    		return((regRule.equals(o.regRule)) && (cTerminus == o.cTerminus));
    	}else
    		return false;
    }
    public ArrayList<Integer> cleaveIndices(String stringseq){
    	// finds indices of all cleavage sites within stringseq for this rule
    	//TODO: there are issues with this, specifically if cTerminus positive and cleavage comes before the rule,
    	// oddity.
        ArrayList<Integer> Splits = new ArrayList<Integer>();
        Iterator<String> Iter;
        int current = 0;
        int indReg = 0;
        int indExcept = -1;
        String tempExcept;
        //VGKYP
        //01234
        
        // two cases, cleave in one direction or in the other, handled differently
        if(cTerminus == true){
        	// finds cleavage site for both regular expression and it's exception and see's if they match,
        	// if match skip, if not match record into split... proceed until no more
            while(indReg!= -1){ // indexOf returns -1 if not found
            	Iter = RuleExceptions.iterator();
                indReg = stringseq.indexOf(regRule,current); // current is incremented along and denotes last found
                    if(indReg != -1){
                        indReg +=regRule.length(); // length of rule factored in to find index of cleavage site
                        while(Iter.hasNext()&&indReg!= indExcept){
                            tempExcept = Iter.next();
                            indExcept = stringseq.indexOf(tempExcept,current)+tempExcept.length();
                        }
                        if(indExcept != indReg){
                            Splits.add(indReg);
                        }
                        current = indReg;
                    }
            }
        }else{
            while(indReg!= -1){
            	// same idea as above, but different computation of cleavage sites and exceptions
                Iter = RuleExceptions.iterator();
                if(current+regRule.length()>stringseq.length()-1){
                    return Splits;
                }
                indReg = stringseq.indexOf(regRule,current+regRule.length());
                    if(indReg != -1){
                        while(Iter.hasNext()&&indReg!= indExcept){
                            tempExcept = Iter.next();
                            indExcept = stringseq.indexOf(tempExcept,current+regRule.length());
                        }
                        if(indExcept != indReg){
                            Splits.add(indReg);
                        }
                        current = indReg;
                    }
            }
        }
        return Splits;
    }

}